package com.ylbh.modules.member.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ylbh.common.exception.NormalException;
import com.ylbh.common.pojo.member.dto.ExtensionCount2LevelDTO;
import com.ylbh.common.pojo.member.dto.UpgradeDTO;
import com.ylbh.common.pojo.member.dto.select.UpgradeSelectDTO;
import com.ylbh.common.pojo.member.vo.ExtensionCount2LevelVO;
import com.ylbh.common.pojo.member.vo.ExtensionCountVO;
import com.ylbh.common.pojo.member.vo.UpgradeExcelVO;
import com.ylbh.common.pojo.member.vo.UpgradeTypeVO;
import com.ylbh.common.utils.ExcelUtils;
import com.ylbh.common.utils.PageUtils;
import com.ylbh.modules.member.mapper.UpgradeMapper;
import com.ylbh.modules.member.service.UpgradeService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 消费者列表
 */
@Service("upgradeService")
public class UpgradeServiceImpl extends ServiceImpl<UpgradeMapper, UpgradeDTO> implements UpgradeService {
    @Autowired
    private UpgradeMapper upgradeMapper;

    /**
     * 消费者数据列表
     * @param params
     * @return
     */
    @Override
    public PageUtils queryPage(UpgradeSelectDTO params) {
        Page<Object> page = new Page<>(params.getPage(), params.getLimit());
        List<UpgradeDTO> list = upgradeMapper.selectUpgradeList(params, page);
        return new PageUtils(new Page<UpgradeDTO>().setTotal(page.getTotal()).setRecords(list));
    }

    /**
     * 会员推广统计
     * @param params
     * @return
     */
    @Override
    public List<ExtensionCountVO> extensionCount(UpgradeSelectDTO params) {
        return upgradeMapper.selectExtensionCount(params);
    }

    /**
     * 会员推广统计 二级
     * @param params
     * @return
     */
    @Override
    public List<ExtensionCount2LevelVO> extensionCount2Level(UpgradeSelectDTO params) {
        List<ExtensionCount2LevelDTO> count2Levels = upgradeMapper.selectExtensionCount2Level(params);
        int paltForm = 0;//平台升级
        int countinue = 0;//平台续费
        int underLine = 0;//线下交钱
        int unionCityPartner = 0;//联盟店城市合伙人
        int cityPartner = 0;//城市合伙人
        int heightStore = 0;//高级联盟店
        int normalStore = 0;//普通联盟店
        int store = 0;//联盟店
        int vipMembers = 0;//vip会员
        int normalCousumer = 0;//普通消费者
        int areas = 0;//运营商自己
        for(ExtensionCount2LevelDTO extensionCount2LevelDTO : count2Levels){
            if(extensionCount2LevelDTO ==null){
                continue;
            }
            if(StringUtils.isNotBlank(extensionCount2LevelDTO.getMoneyType()) && "1".equals(extensionCount2LevelDTO.getMoneyType())){
                paltForm++;
            }else if(StringUtils.isNotBlank(extensionCount2LevelDTO.getMoneyType()) && "2".equals(extensionCount2LevelDTO.getMoneyType())){
                countinue++;
            }else if(StringUtils.isNotBlank(extensionCount2LevelDTO.getMoneyType()) && "3".equals(extensionCount2LevelDTO.getMoneyType())){
                underLine++;
            }else if(StringUtils.isNotBlank(extensionCount2LevelDTO.getRecommUserType()) && "8".equals(extensionCount2LevelDTO.getRecommUserType())){
                unionCityPartner ++;
            }else if(StringUtils.isNotBlank(extensionCount2LevelDTO.getRecommUserType()) && "7".equals(extensionCount2LevelDTO.getRecommUserType())){
                cityPartner++;
            }else if(StringUtils.isNotBlank(extensionCount2LevelDTO.getRecommUserType()) && "6".equals(extensionCount2LevelDTO.getRecommUserType())){
                heightStore++;
            }else if(StringUtils.isNotBlank(extensionCount2LevelDTO.getRecommUserType()) && "5".equals(extensionCount2LevelDTO.getRecommUserType())){
                normalStore++;
            }else if(StringUtils.isNotBlank(extensionCount2LevelDTO.getRecommUserType()) && "4".equals(extensionCount2LevelDTO.getRecommUserType())){
                vipMembers++;
            }else if(StringUtils.isNotBlank(extensionCount2LevelDTO.getRecommUserType()) && "3".equals(extensionCount2LevelDTO.getRecommUserType())){
                normalCousumer++;
            }else if(StringUtils.isNotBlank(extensionCount2LevelDTO.getRecommUserType()) && "2".equals(extensionCount2LevelDTO.getRecommUserType())){
                store++;
            }else if(StringUtils.isNotBlank(extensionCount2LevelDTO.getRecommUserType()) && "1".equals(extensionCount2LevelDTO.getRecommUserType())){
                areas++;
            }
        }
        ExtensionCount2LevelVO count2Level = new ExtensionCount2LevelVO();
        List<ExtensionCount2LevelVO> data =new ArrayList<>();
        count2Level.setNum(paltForm);
        count2Level.setType("paltForm");
        data.add(count2Level);

        count2Level = new ExtensionCount2LevelVO();
        count2Level.setType("countinue");
        count2Level.setNum(countinue);
        data.add(count2Level);

        count2Level = new ExtensionCount2LevelVO();
        count2Level.setType("underLine");
        count2Level.setNum(underLine);
        data.add(count2Level);

        count2Level = new ExtensionCount2LevelVO();
        count2Level.setType("unionCityPartner");
        count2Level.setNum(unionCityPartner);
        data.add(count2Level);

        count2Level = new ExtensionCount2LevelVO();
        count2Level.setType("cityPartner");
        count2Level.setNum(cityPartner);
        data.add(count2Level);

        count2Level = new ExtensionCount2LevelVO();
        count2Level.setType("cityPartner");
        count2Level.setNum(cityPartner);
        data.add(count2Level);

        count2Level = new ExtensionCount2LevelVO();
        count2Level.setType("heightStore");
        count2Level.setNum(heightStore);
        data.add(count2Level);

        count2Level = new ExtensionCount2LevelVO();
        count2Level.setType("normalStore");
        count2Level.setNum(normalStore);
        data.add(count2Level);

        count2Level = new ExtensionCount2LevelVO();
        count2Level.setType("store");
        count2Level.setNum(store);
        data.add(count2Level);

        count2Level = new ExtensionCount2LevelVO();
        count2Level.setType("vipMembers");
        count2Level.setNum(vipMembers);
        data.add(count2Level);

        count2Level = new ExtensionCount2LevelVO();
        count2Level.setType("normalCousumer");
        count2Level.setNum(normalCousumer);
        data.add(count2Level);

        count2Level = new ExtensionCount2LevelVO();
        count2Level.setType("areas");
        count2Level.setNum(areas);
        data.add(count2Level);

        return data;
    }

    /**
     * 会员升级导出
     * @param response
     * @param request
     */
    @Override
    public void upgradeExcel(HttpServletResponse response, HttpServletRequest request) throws NormalException {
        String isPay = request.getParameter("isPay");
        String keywords = request.getParameter("keywords");
        String startTime = request.getParameter("startTime");
        String endTime = request.getParameter("endTime");
        List<UpgradeExcelVO> excel = upgradeMapper.upgradeExcel(isPay, keywords, startTime, endTime);
        ExcelUtils.exportExcel(excel,"会员升级","",UpgradeExcelVO.class,System.currentTimeMillis()+".xls",response);
    }

    @Override
    public List queryExtensionList(UpgradeSelectDTO dto) {
        List upgradeList = null;
        if(StringUtils.isEmpty(dto.getKeywords())) {
            upgradeList = upgradeMapper.selectMembershipPromotionList(dto);
        } else {
//            upgradeList = upgradeMapper.selectMembershipPromotionsList(dto);
        }
        return upgradeList;
    }

    @Override
    public List queryUpgradeType(UpgradeSelectDTO dto) {
        List<UpgradeTypeVO> list = upgradeMapper.selectUpgradeType(dto);
        List<Integer> list1 = new ArrayList<>();
        // 平台升级
        int platformUpgrade = 0;
        // 平台续费
        int platformRenewal = 0;
        // 下线交钱
        int offlinePayment = 0;
        // 运营商
        int operators = 0;
        // 普通联盟店
        int generalAllianceStore = 0;
        // 普通消费者
        int ordinaryConsumers = 0;
        // vip金牌会员
        int vIPGoldMembers = 0;
        // 中级联盟店
        int midLevelAllianceStore = 0;
        // 高级联盟店
        int superiorAllianceStore = 0;
        // 城市合伙人
        int cityPartner = 0;
        // 城市合伙人联盟店
        int cityPartnerAllianceStore = 0;
        for (UpgradeTypeVO vo : list) {
            if (null != vo.getMoneyType() && vo.getMoneyType() == 1) {
                platformUpgrade ++;
            } else if (null != vo.getMoneyType() && vo.getMoneyType() == 2) {
                platformRenewal ++;
            } else if (null != vo.getMoneyType() && vo.getMoneyType() == 3) {
                offlinePayment ++;
            } else if (vo.getRecommUserType() == 1) {
                operators ++;
            } else if (vo.getRecommUserType() == 2) {
                generalAllianceStore ++;
            } else if (vo.getRecommUserType() == 3) {
                ordinaryConsumers ++;
            } else if (vo.getRecommUserType() == 4) {
                vIPGoldMembers ++;
            } else if (vo.getRecommUserType() == 5) {
                midLevelAllianceStore ++;
            } else if (vo.getRecommUserType() == 6) {
                superiorAllianceStore ++;
            } else if (vo.getRecommUserType() == 7) {
                cityPartner ++;
            } else if (vo.getRecommUserType() == 8) {
                cityPartnerAllianceStore ++;
            }
        }
        list1.add(platformUpgrade);
        list1.add(platformRenewal);
        list1.add(offlinePayment);
        list1.add(operators);
        list1.add(generalAllianceStore);
        list1.add(ordinaryConsumers);
        list1.add(vIPGoldMembers);
        list1.add(midLevelAllianceStore);
        list1.add(superiorAllianceStore);
        list1.add(cityPartner);
        list1.add(cityPartnerAllianceStore);
        return list1;
    }
}
